﻿using BXH.Movie.Bll;
using BXH.Movie.FrmUsers;
using BXH.Movie.Model;
using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BXH.Movie.Forms
{
    public partial class UCSeatManager : UserControl
    {
        private List<Point> points = new List<Point>();
        private MovieInfoDto _movieInfo;
        public event EventHandler OrderNotLogin;//购票没有登录的事件
        private OrderInfoService orderInfoService = new OrderInfoService();
        private MovieSessionService sessionService = new MovieSessionService();
        private int currentSessionId = 0;
        public UCSeatManager(MovieInfoDto movieInfo)
        {
            InitializeComponent();
            _movieInfo = movieInfo;

        }

        private void SeatManager_Load(object sender, EventArgs e)
        {
            lbName.Text = _movieInfo.Name;
            lbTime.Text = _movieInfo.Time + "分钟";

            var imagePath = Path.Combine(FrmMain.BasePath, _movieInfo.Image);
            PicLogo.Image = Image.FromFile(imagePath);

            var sessions = sessionService.GetSessionByMovieId(_movieInfo.Id);
            if (sessions.Count()<=0)
            {
                MessageBoxEx.Show("当前没有场次");
                return;
            }
              
            comboBox1.DataSource = null;
            comboBox1.DataSource = sessions;
            comboBox1.SelectedIndex = 0;
            comboBox1.DisplayMember = "BroadcastTime";
            this.btnOrder.Enabled = true;

        }
        private void LoadSeat(int sessionId)
        {
            plSeat.Controls.Clear();

            var orders = orderInfoService.GetOrderSeatBySessionId(sessionId);

            var positions = new List<Point>();
            foreach (var item in orders)
            {
                var pointStr = item.Seat.Split(',');
                positions.Add(new Point { X = Convert.ToInt32(pointStr[0]), Y = Convert.ToInt32(pointStr[1]) });
            }

            int iRow = 10;
            int iColumn = 12;

            //ToolTip toolTip = new ToolTip();

            for (int i = 0; i < iRow; i++)
            {
                //var vResult = (i + 1) + "排";
                int iRowLocation = 50 + (i * 42);
                for (int j = 0; j < iColumn; j++)
                {

                    var position = new Point { X = i + 1, Y = j + 1 };// vResult + (j + 1) + "座";

                    int iColumnLocation = 10 + (j * 42);
                    CheckBox _CheckBox = new CheckBox();
                    _CheckBox.Appearance = System.Windows.Forms.Appearance.Button;
                    _CheckBox.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
                    _CheckBox.FlatAppearance.BorderSize = 0;
                    _CheckBox.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                    _CheckBox.Location = new System.Drawing.Point(iColumnLocation, iRowLocation);
                    _CheckBox.Name = "CkbSeat1";
                    _CheckBox.Size = new System.Drawing.Size(32, 32);
                    _CheckBox.TabIndex = 0;
                    _CheckBox.Tag = position;
                    _CheckBox.UseVisualStyleBackColor = true;
                    _CheckBox.MouseClick += _CheckBox_MouseClick;

                    if (positions.Contains(position))
                    {
                        _CheckBox.BackgroundImage = Properties.Resources.Seat2;
                        _CheckBox.Enabled = false;
                    }
                    else
                    {
                        _CheckBox.BackgroundImage = Properties.Resources.Seat0;
                    }
                    //toolTip = new ToolTip();
                    //toolTip.InitialDelay = 200;
                    //toolTip.AutoPopDelay = 10 * 1000;
                    //toolTip.ReshowDelay = 200;
                    //toolTip.ShowAlways = true;
                    //toolTip.IsBalloon = true;

                    //string tipoverwrite = vResult1;
                    //toolTip.SetToolTip(_CheckBox, tipoverwrite); // ckboverwrite is a checkbox

                    this.plSeat.Controls.Add(_CheckBox);
                }
            }
        }
        private void _CheckBox_MouseClick(object sender, MouseEventArgs e)
        {
            var selectPos = sender as CheckBox;
            var point = (Point)selectPos.Tag;
            if (selectPos.CheckState == CheckState.Checked)
            {
                selectPos.BackgroundImage = Properties.Resources.Seat1;
                if (!points.Exists(p => p.X == point.X && p.Y == point.Y))
                {
                    points.Add(point);
                }

            }
            else
            {
                if (points.Exists(p => p.X == point.X && p.Y == point.Y))
                {
                    points.RemoveAll(p => p.X == point.X && p.Y == point.Y);
                }
                selectPos.BackgroundImage = Properties.Resources.Seat0;
            }
        }
        /// <summary>
        /// 购票
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOrder_CheckedChanged(object sender, EventArgs e)
        {
            if (Program.LoginUser == null)
            {//没有登录
                OrderNotLogin?.Invoke(this, new EventArgs());
                FrmMain._FrmMain.GoToLogin();
            }
            if (Program.LoginUser == null)//没有登录
                return;
            List<OrderInfoInput> orderInfoInputs = new List<OrderInfoInput>();
            foreach (var point in points)
            {
                orderInfoInputs.Add(new OrderInfoInput
                {
                    Seat = $"{point.X},{point.Y}",
                    SessionId = currentSessionId,
                    UserId = Program.LoginUser.Id
                });
            }
            UCOrder ucOrder = new UCOrder(orderInfoInputs, _movieInfo);
            FrmMain._FrmMain.SetManageOtherShow(ucOrder);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MovieSession session = comboBox1.SelectedItem as MovieSession;
            LoadSeat(session.Id);
            currentSessionId = session.Id;
        }
    }
}
